import store from "@/store/index.js"
const cart = {
	state: {
		list: [],
		selectedList: []
	},
	mutations: {
		//全选按钮
		checkAll(state) {
			if (this.getters.checkedAll) {
				state.list.map(v => {
					v.checked = false
				})
				state.selectedList = []
			} else {
				state.selectedList = state.list.map(v => {
					v.checked = true
					return v.id
				})
			}
			//持久化保存数据
			let openId = store.state.user.userInfo.openId || store.state.user.userInfo.phone
			uni.setStorageSync(openId + 'cart',JSON.stringify(state.list))
			uni.setStorageSync(openId + 'length',JSON.stringify(state.selectedList))
		},
		//单选按钮
		checkedFn(state, index) {
			if (state.list[index].checked) {
				state.selectedList = state.selectedList.filter(v => {
					if (v === state.list[index].id) {
						return false
					} else {
						return v
					}
				})
			} else {
				state.selectedList.push(state.list[index].id)
			}
			state.list[index].checked = !state.list[index].checked
			//持久化保存数据
			let openId = store.state.user.userInfo.openId || store.state.user.userInfo.phone
			uni.setStorageSync(openId + 'cart',JSON.stringify(state.list))
			uni.setStorageSync(openId + 'length',JSON.stringify(state.selectedList))
		},
		//编辑单个商品数量
		changeNumber(state, val) {
			let i = val.payload.index
			let count = val.payload.e
			state.list[i].num = count
			state.list[i].price = state.list[i].pprice * count
			//持久化保存数据
			let openId = store.state.user.userInfo.openId || store.state.user.userInfo.phone
			uni.setStorageSync(openId + 'cart',JSON.stringify(state.list))
			uni.setStorageSync(openId + 'length',JSON.stringify(state.selectedList))
		},
		//删除购物车数据
		delGoods(state) {
			if(!state.selectedList.length){
				return uni.showToast({
					title:"请选择商品",
					icon:"none"
				})
			}
			state.list = state.list.filter(v => {
				let index = state.selectedList.indexOf(v.id)
				if (index === -1) {
					return v
				} else {
					state.selectedList.splice(index, 1)
					return false
				}
			})
			uni.showToast({
				title: "删除成功",
				icon: "none"
			})
			//持久化保存数据
			let openId = store.state.user.userInfo.openId || store.state.user.userInfo.phone
			uni.setStorageSync(openId + 'cart',JSON.stringify(state.list))
			uni.setStorageSync(openId + 'length',JSON.stringify(state.selectedList))
		},
		//提交订单删除购物车商品
		
		//加入购物车
		addShopCart(state, goods) {
			//判断是否已经有相同的数据
			if (!state.list.length) {
				//第一次直接添加
				state.list.push(goods)
				//持久化保存数据
				let openId = store.state.user.userInfo.openId || store.state.user.userInfo.phone
				uni.setStorageSync(openId + 'length',JSON.stringify(state.selectedList))
				return uni.setStorageSync(openId + 'cart',JSON.stringify(state.list))
			}
			let key = true
			state.list.forEach((v, i) => {
				if (v.id == goods.id) {
					//已经存在相同商品,累加商品数量
					key = false
					state.list[i].num = Number(state.list[i].num) + Number(goods.num)
				}
			})
			//不存在商品则添加
			if(key){
				state.list.push(goods)
			}
			//持久化保存数据
			let openId = store.state.user.userInfo.openId || store.state.user.userInfo.phone
			uni.setStorageSync(openId + 'cart',JSON.stringify(state.list))
			uni.setStorageSync(openId + 'length',JSON.stringify(state.selectedList))
		},
		//读取购物车数据
		initCart(state){
			let openId = store.state.user.userInfo.openId || store.state.user.userInfo.phone
			let cartInfo = uni.getStorageSync(openId + 'cart')
			let cartLength = uni.getStorageSync(openId + 'length')
			if(cartInfo){
				state.list = JSON.parse(cartInfo)
			}
			if(cartLength){
				state.selectedList = JSON.parse(cartLength)
			}
		},
		//跳转结算
		goConfirmOrder(state){
			if(!state.selectedList.length){
				return uni.showToast({
					title:"请选择商品",
					icon:"none"
				})
			}
			uni.navigateTo({
				url:"/pages/confirm-order/confirm-order"
			})
		}
	},
	actions: {
		delGoodsFn(context) {
			context.commit("delGoods")
		}
	},
	getters: {
		// 计算是否全选
		checkedAll(state) {
			return state.list.length === state.selectedList.length
		},
		//结算数量
		totalCount(state) {
			return state.selectedList.length
		},
		//总价格
		totalPrice(state) {
			let price = 0
			state.list.forEach(v => {
				if (v.checked) {
					let price_ = v.price ? v.price : v.pprice
					price += Number(price_);
				}
			})
			return price
		},
		list(state){
			return state.list
		}
	}
}
export default cart
